package com.elm.view.impl;

import com.elm.dao.FoodDao;
import com.elm.dao.impl.FoodDaoImpl;
import com.elm.entity.Food;
import com.elm.view.FoodView;

import java.util.List;
import java.util.Scanner;

public class FoodViewImpl implements FoodView {
    private Scanner input = new Scanner(System.in);

    @Override
    public void listFoodByBusinessId(Integer businessId) {
        FoodDao dao = new FoodDaoImpl();
        List<Food> list = dao.listFoodByBusinessId(businessId);
        System.out.println("食品编号\t食品名称\t食品介绍\t食品价格");
        for (Food food : list) {
            System.out.println(food.getFoodId() + "\t" + food.getFoodName() + "\t" + food.getFoodExplain() + "\t" + food.getFoodPrice());
        }
    }

    @Override
    public void saveFood(Integer businessId) {
        Food food = new Food();
        System.out.println("请输入食品名称：");
        food.setFoodName(input.next());
        System.out.println("请输入食品介绍：");
        food.setFoodExplain(input.next());
        System.out.println("请输入食品价格：");
        food.setFoodPrice(input.nextDouble());
        food.setBusinessId(businessId);

        FoodDao dao = new FoodDaoImpl();
        int result = dao.saveFood(food);
        if (result > 0) {
            System.out.println("新增食品成功！");
        } else {
            System.out.println("新增食品失败！");
        }
    }

    @Override
    public void updateFood(Integer businessId) {
        FoodDao dao = new FoodDaoImpl();
        List<Food> list = dao.listFoodByBusinessId(businessId);
        if (list.size() == 0) {
            System.out.println("该商家下没有食品！");
            return;
        }

        System.out.println("请输入要修改的食品编号：");
        Integer foodId = input.nextInt();
        Food food = dao.getFoodById(foodId);
        if (food == null) {
            System.out.println("食品不存在！");
            return;
        }

        System.out.println("请输入新的食品名称（" + food.getFoodName() + "）：");
        food.setFoodName(input.next());
        System.out.println("请输入新的食品介绍（" + food.getFoodExplain() + "）：");
        food.setFoodExplain(input.next());
        System.out.println("请输入新的食品价格（" + food.getFoodPrice() + "）：");
        food.setFoodPrice(input.nextDouble());

        int result = dao.updateFood(food);
        if (result > 0) {
            System.out.println("食品信息修改成功！");
        } else {
            System.out.println("食品信息修改失败！");
        }
    }

    @Override
    public void removeFood(Integer businessId) {
        FoodDao dao = new FoodDaoImpl();
        List<Food> list = dao.listFoodByBusinessId(businessId);
        if (list.size() == 0) {
            System.out.println("该商家下没有食品！");
            return;
        }

        System.out.println("请输入要删除的食品编号：");
        Integer foodId = input.nextInt();
        int result = dao.removeFood(foodId);
        if (result > 0) {
            System.out.println("删除食品成功！");
        } else {
            System.out.println("删除食品失败！");
        }
    }
}
